import {getUserTokenFromLocal} from "./utils/LocalStorageUtils";
import {BaseRespData, BaseReq, PlatReq, RedirectDifyReq} from "./types/CommonTypes";
import {message} from "antd";
export const openLoginModal = "openLoginModal";
export const reqHost = "";//"http://121.41.2.116:8001";
export const comFetch=<T>(req: BaseReq):Promise<BaseRespData<T>> =>{
    const headers = req.reqData?.headers;
    if (headers) {
        headers['Content-Type'] = 'application/json';
    }
    const request:Record<string, unknown> = {
        method: req.method,
        headers: headers,
    }
    if (req.method === 'POST' && req.reqData?.body) {
        request["body"] = JSON.stringify(req.reqData?.body);
    }
    return new Promise((resolve,reject) => {
        fetch(reqHost + req.apiUrl, request)
            .then(res => {
                if (!res.ok) {
                    reject('HTTP 请求出错! status:' + res.statusText);
                }
                if (!res.body) {
                    reject('没有可用数据:' + res.statusText);
                }
                if (res.status!=200) {
                    reject('请求失败请稍后重试,' + res.statusText);
                }
                resolve(res.json());
            })
            .catch(err => reject(err));
    });
}
export const comPlatReq= <T>(platReq: PlatReq): Promise<Record<string, T>> => {
    return new Promise((resolve,reject)=>{
        const intfName = platReq.interfaceName;
        const userTokenFromLocal = getUserTokenFromLocal();
        if (!userTokenFromLocal && intfName!="smsCodeLogin" && intfName!="sendLoginSmsCode") {
            //没有登陆需要登陆
            reject(openLoginModal);
        }
        const headers={"rToken":userTokenFromLocal || ""};
        const req: BaseReq= {
            apiUrl: "/unifiedApi/commonEnter",
            method: "POST",
            reqData: {
                headers: headers,
                body: platReq,
            },
        }
        comFetch(req)
            .then((data)=>{
                if (data.errorCode && data.errorMsg) {
                    if (data.errorCode.includes("401")) {
                        message.error(data.errorMsg);
                        reject(openLoginModal);
                    } else {
                        reject("【"+data.errorCode+"】"+ data.errorMsg);
                    }
                    return;
                }
                resolve(data.data as Record<string, T>);
            })
            .catch(err => reject(err));
    });
}
export const difyHttpRedirect =<T>(difyReq: RedirectDifyReq):Promise<T> =>{
    return new Promise((resolve,reject)=>{
        const platReq: PlatReq = {
            interfaceName: "commonDifyHttpRedirect",
            reqData: {
                url: difyReq.difyUrl,
                method: difyReq.method,
                body: difyReq.difyReqBody,
                parameters: difyReq.difyReqParam
            },
        }
        const defaultResDataField = difyReq.defaultResDataField;
        comPlatReq(platReq)
            .then(data=>{
                if (defaultResDataField) {
                    resolve(data[defaultResDataField] as T);
                } else {
                    resolve(data as T);
                }
            }).catch(err => reject(err));
    })
}